// 引入
import "./tour";

function readXML(filePath) {
  // 创建一个新的xhr对象
  let xhr = null;
  if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
  } else {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhr.open("GET", filePath, false);
  xhr.overrideMimeType("text/html;charset=utf-8");
  xhr.send(null);
  let div = document.createElement("div");
  div.innerHTML = xhr.responseText.replace(/\s*/g, "");
  console.log(div);
  // console.log(xhr.responseText.replace(/\s*/g, ""));
  // if (typeof xhr.responseText === "string")
  // console.log(div.childNodes);
  // htmlDeal(div.childNodes[0].childNodes);
}

function htmlDeal(data) {
  console.log(data);
  data.forEach(item => {
    console.log(item.nodeName);
    if (item.nodeName === "LBS") {
      let list = [];
      item.childNodes.forEach(item1 => {
        let obj = {};
        if (item1.nodeName === "LB") {
          item1.childNodes.forEach(item2 => {
            if (item2.nodeName === "LABEL") {
              obj.label = item2.textContent;
            } else if (item2.nodeName === "TEXT") {
              obj.value = item2.textContent;
            }
          });
        }
        list.push(obj);
      });
      // this.titleList = list;
      console.log("title", list);
    } else if (item.nodeName === "TABLE") {
      let list = [];
      item.childNodes.forEach(item1 => {
        let obj = {};
        if (item1.nodeName === "THEAD") {
          item1.childNodes.forEach(item2 => {
            let aaa = [];
            if (item2.nodeName === "TR") {
              item2.childNodes.forEach((item3, index3) => {
                let obj1 = {};
                if (item3.nodeName === "TH") {
                  obj1.value = item3.textContent;
                }
                aaa.push(obj1);
              });
            }
            obj.head === aaa;
            list.push(obj);
          });
        } else if (item1.nodeName === "TBODY") {
          let bbb = [];
          item1.childNodes.forEach(item2 => {
            if (item2.nodeName === "TR") {
              let ccc = [];
              item2.childNodes.forEach((item3, index3) => {
                let obj2 = {};
                if (item3.nodeName === "TD") {
                  console.log(item3.childNodes.length);
                  if (item3.childNodes && item3.childNodes.length > 1) {
                    item3.childNodes.forEach(item4 => {
                      if (item4.nodeName === "TEXT")
                        obj2.value = item4.textContent;
                      else if (item4.nodeName === "EDIT")
                        obj2.edit = item4.textContent;
                    });
                  } else {
                    obj2.value = item3.tetContent;
                  }
                }
                ccc.push(obj2);
              });
              bbb.push(ccc);
            }
          });
          obj.body = bbb;
          list.push(obj);
          console.log("table", list);
          // this.tableList = list;
        }
      });
    }
  });
}


// 导出
export { readXML, htmlDeal };
